package com.ibm.service;

import com.ibm.dao.UserDAO;
import com.ibm.entity.User;
import com.ibm.utils.MD5Utils;

import java.util.List;

public class UserService {
    private UserDAO userDAO=new UserDAO();

    //查询所有用户
    public  List<User> listUser()
    {
        List<User> userList=userDAO.selectUsers();
        return userList;
    }
    //保存用户
    public  boolean saveUser(User user)
    {
        String s=com.ibm.utils.MD5Utils.md5Encode(user.getPwd());
        user.setPwd(s);
        int i=UserDAO.insertUsers(user);
        return i>0;
    }
    //删除用户
    public  boolean deleteUser(int userId)
    {
        int i=userDAO.deleteUser(userId);

        return i>0;
    }
    //查用户by ID
    public User getUserById(int userId)
    {
        User user=userDAO.selectUserById(userId);
        return user;
    }
    //修改用户信息
    public  boolean updateUser(User user)
    {

        if(user.getPwd()==null||"".equals(user.getPwd()))
        {
            User oldUser=userDAO.selectUserById(user.getUserId());
            user.setPwd(oldUser.getPwd());
        }
        else
        {
            String s=com.ibm.utils.MD5Utils.md5Encode((user.getPwd()));
            user.setPwd(s);
        }
        int i=userDAO.updateUser(user);

        return i>0;
    }



}
